package client;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class EditWindow extends JFrame {

	/* Testing */
	public static void main(String args[]) {
		EditWindow w = new EditWindow();
		String e = w.editText("This is ma text");
		w.setVisible(false);
		w.showText("HEAHEHAE: " + e);
		w.setVisible(false);
		System.out.println("Got: " + e);
		System.exit(0);
	}

	private JPanel buttPanel = new JPanel();
	private Button okButt = new Button("Save");
	private Button cancelButt = new Button("Cancel");
	private JTextArea textArea = new JTextArea();
	private JPanel allPanel = new JPanel();

	boolean sleep = false;
	boolean edited = false;

	public EditWindow() {
		allPanel.setLayout(new BoxLayout(allPanel, BoxLayout.Y_AXIS));

		buttPanel.add(cancelButt);
		textArea.setSize(800, 600);
		allPanel.add(textArea);
		allPanel.add(buttPanel);
		this.add(allPanel);
		okButt.addActionListener(new OkListener());
		cancelButt.addActionListener(new CancelListener());
	}

	public void showText(String s) {
		buttPanel.remove(okButt);
		textArea.setText(s);
		this.setVisible(true);

		this.setSize(1000, 800);
		sleep = true;
		while (sleep) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.setVisible(false);
	}

	public String editText(String s) {
		buttPanel.add(okButt);
		textArea.setText(s);
		this.setVisible(true);

		this.setSize(1000, 800);
		sleep = true;
		while (sleep) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				this.setVisible(false);
				return null;
			}
		}

		this.setVisible(false);
		if (edited) {
			return textArea.getText();
		} else {
			return "";
		}

	}

	class OkListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			edited = true;
			sleep = false;
		}

	}

	class CancelListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			edited = false;
			sleep = false;
		}

	}
}
